<template>
  <div class="mod-config">
    <!-- 操作表单 -->
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.keyword" placeholder="请输入标题" clearable @clear="getDataList()"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">搜索</el-button>
        <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button
          type="danger"
          @click="deleteHandle()"
          :disabled="dataListSelections.length <= 0"
        >批量删除</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格 -->
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;"
    >
      <el-table-column type="selection" align="center" width="50" />
      <el-table-column align="center" type="index" width="80" label="序号" />
      <el-table-column prop="title" label="标题" align="center" />
      <el-table-column prop="createTime" label="发布时间" align="center" />
      <!-- <el-table-column prop="sort" label="排序" align="center" /> -->

      <el-table-column prop="isDisplay" label="是否显示" align="center" width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isDisplay === 1">是</el-tag>
          <el-tag type="warning" v-else>否</el-tag>
        </template>
      </el-table-column>

      <!-- <el-table-column prop="isRecommend" label="是否推荐" align="center" width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isRecommend === 1">是</el-tag>
          <el-tag type="warning" v-else>否</el-tag>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="是否推荐" header-align="center" align="center" width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isRecommend === 0">否</el-tag>
          <el-tag v-else-if="scope.row.isRecommend === 1" type="success">是</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="isTop" label="是否置顶" align="center" width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isTop === 1">是</el-tag>
          <el-tag type="warning" v-else>否</el-tag>
        </template>
      </el-table-column> -->

      <el-table-column fixed="right" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="text" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from './news-building-add'

export default {
  data() {
    return {
      dataForm: {
        keyword: ''
      },
      type: 2,
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false
    }
  },
  components: {
    AddOrUpdate
  },
  mounted() {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    async getDataList(isSearch) {
      this.dataListLoading = true
      var res = await this.$http.post('/news-admin/news/list', {
        page: this.pageIndex,
        limit: this.pageSize,
        title: this.dataForm.keyword,
        type: this.type
      })
      this.dataList = res.data.list
      this.totalPage = res.data.totalCount
      this.dataListLoading = false
    },
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => this.$refs.addOrUpdate.initEditor(id))
    },
    // 删除
    async deleteHandle(id) {
      var ids = id || this.dataListSelections.map(item => item.id).join(',')
      var confirm = await this.$confirm(`确定要进行删除操作吗?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      if (confirm !== 'confirm') return

      let res = await this.$http({
        url: '/news-admin/news/delete',
        method: 'post',
        data: {
          ids: ids
        }
      })
      if (res && res.code === 200) {
        this.$notify.success({ title: '成功', message: '删除党建新闻成功' })
        this.pageIndex > 1 && this.dataList.length <= 1 && this.pageIndex --
        this.getDataList()
      } else {
        this.$notify.error({ title: '错误', message: res.msg })
      }
    }
  }
}
</script>

<style scoped>
</style>
